﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using YwangHRMSys.DBUtility;


public class NationalitiesInfoDAL
    {
        #region 开始生成tomodel提高代码的重用性

        /// 开始生成tomodel提高代码的重用性
        private NationalitiesInfoModel ToModel( DataRow row )
        {
            var model = new NationalitiesInfoModel();
            model.id = (Guid) SqlHelper.FromDbValue(row["id"]);
            model.Number = (Int32) SqlHelper.FromDbValue(row["Number"]);
            model.NationalitiesNames = (String) SqlHelper.FromDbValue(row["NationalitiesNames"]);
            return model;
        } //tomodel的结束。

        #endregion

        #region listAll开始

        //查询所有NationalitiesInfo的数据列表
        public IEnumerable <NationalitiesInfoModel> ListAll()
        {
            var list = new List <NationalitiesInfoModel>();
            DataTable table = SqlHelper.ExecuteDataTable("Select *  from NationalitiesInfo   ORDER BY number ASC ");
            foreach (DataRow row in table.Rows)
                {
                    NationalitiesInfoModel model = ToModel(row);
                    list.Add(model);
                }
            return list;
        } //listAllNationalitiesInfo结束

        #endregion

        #region  根据ID来查询NationalitiesInfo的信息

        public NationalitiesInfoModel GetByNationalitiesInfoID( Guid Id )
        {
            DataTable table = SqlHelper.ExecuteDataTable
                    (
                            " SELECT id,Number,NationalitiesNames FROM  NationalitiesInfo  WHERE  id = @id",
                            new SqlParameter("@id", Id));
            if (table.Rows.Count <= 0)
                {
                    return null;
                }
            if (table.Rows.Count == 1)
                {
                    return ToModel(table.Rows[0]);
                }
            throw new Exception();
        }

        #endregion

        #region  根据ID修改NationalitiesInfo的信息

        public void UpdateNationalitiesInfo( NationalitiesInfoModel model )
        {
            SqlHelper.ExecuteNonQuery
                    (
                            "  UPDATE  NationalitiesInfo SET  id = @id,Number = @Number,NationalitiesNames = @NationalitiesNames  WHERE  id = @id",
                            new SqlParameter("@id", model.id),
                            new SqlParameter("@Number", model.Number),
                            new SqlParameter("@NationalitiesNames", model.NationalitiesNames));
        }

        #endregion

        #region 添加NationalitiesInfo信息

        // 添加一条NationalitiesInfo 的信息
        public void InsertNationalitiesInfo( NationalitiesInfoModel model )
        {
            SqlHelper.ExecuteNonQuery
                    (
                            "INSERT INTO NationalitiesInfo  (id,Number,NationalitiesNames  ) values (@id,@Number,@NationalitiesNames)",
                            new SqlParameter("@id", model.id),
                            new SqlParameter("@Number", model.Number),
                            new SqlParameter("@NationalitiesNames", model.NationalitiesNames));
        }

        #endregion

        //根据ID来查询NationalitiesInfo的信息

        //根据ID修改NationalitiesInfo的信息
    }

//当前NationalitiesInfoDAL类结束